
// 封装HTTP请求
function http(url,callback){
  wx.request({
    url: url,
    method:'GET',
    header:{
      "Content-Type": "json"
    },
    success:function(res){
      callback(res.data);
    },
    fail:function(error){
      console.log(error);
    }
  })
}

// 处理小星星
function convertToStarsArray(stars){
  var num = stars.toString().substring(0,1); //字符串切割  40  切成  4
  var arr=[];
  for(var i=1;i<=5;i++){
    if(i<=num){
      arr.push(1)
    }else{
      arr.push(0)
    }
  }
  return arr;
}

//影人处理   刘若英 / 井柏然 / 周冬雨 
function convertToCastString(casts) {
  var castsjoin = "";
  for (var idx in casts) {
    castsjoin = castsjoin + casts[idx].name + " / ";  
  }
  return castsjoin.substring(0, castsjoin.length - 2);  //把后面的空格和/删掉
}
//影人处理函数  图片加名字
function convertToCastInfos(casts) {
  var castsArray = []
  for (var idx in casts) {
    var cast = {
      img: casts[idx].avatars ? casts[idx].avatars.large : "",
      name: casts[idx].name
    }
    castsArray.push(cast);
  }
  return castsArray;
}

module.exports = {
  convertToStarsArray: convertToStarsArray,  //小星星
  http: http,  //请求
  convertToCastString: convertToCastString,
  convertToCastInfos: convertToCastInfos
}